import 'dart:convert';

import 'package:bot_toast/bot_toast.dart';
import 'package:exams_app/gen_a/A.dart';
import 'package:exams_app/login_module/view.dart';
import 'package:exams_app/net/Address.dart';
import 'package:exams_app/net/DioManager.dart';
import 'package:exams_app/net/persisten_storage.dart';
import 'package:exams_app/widget/hexcolor.dart';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
class RegisterSetPasswordPage extends StatefulWidget {


  var login;
  var verifyCode;

  RegisterSetPasswordPage(this.login,this.verifyCode);


  @override
  _RegisterSetPasswordPageState createState() => _RegisterSetPasswordPageState();
}

class _RegisterSetPasswordPageState extends State<RegisterSetPasswordPage> with SingleTickerProviderStateMixin {


  var tf1 = TextEditingController();
  var tf2 = TextEditingController();




  @override
  void initState() {
    super.initState();

  }

  @override
  void dispose() {

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: new IconButton(
          icon: Image.asset(A.assets_arrback_icon),
          onPressed: () => {
            Navigator.of(context).pop('刷新')
          },
        ),
      ),
      body: Column(
        crossAxisAlignment:CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.only(top: 50,left: 15),
            child: Text('设置登录密码',style: TextStyle(fontSize: 24,fontWeight: FontWeight.w500),),

          ),

          Container(
            margin: EdgeInsets.only(top: 10,left: 20),
            child: Column(
              children: [
                // TextField(
                //   controller: tf3,
                //   obscureText : false,
                //
                //   style: TextStyle(fontSize: 24,color: HexColor('#242A36')),
                //   decoration: InputDecoration(
                //
                //       hintStyle: TextStyle(fontSize: 24,color: HexColor('#CCCCCC')),
                //       border: InputBorder.none,
                //       hintText: '项目名称'
                //   ),
                // ),
                // Container(height: 1,color: HexColor('#EEEEEE'),margin: EdgeInsets.only(right: 15),),
                //
                // TextField(
                //   controller: tf4,
                //   obscureText : false,
                //
                //   style: TextStyle(fontSize: 24,color: HexColor('#242A36')),
                //   decoration: InputDecoration(
                //
                //       hintStyle: TextStyle(fontSize: 24,color: HexColor('#CCCCCC')),
                //       border: InputBorder.none,
                //       hintText: '单位名称'
                //   ),
                // ),
                // Container(height: 1,color: HexColor('#EEEEEE'),margin: EdgeInsets.only(right: 15),),


                TextField(
                  controller: tf1,
                  obscureText : true,
                  inputFormatters: [
                    WhitelistingTextInputFormatter(RegExp(
                        "[a-zA-Z]|[0-9]")),
                    LengthLimitingTextInputFormatter(16)
                  ],
                  style: TextStyle(fontSize: 24,color: HexColor('#242A36')),
                  decoration: InputDecoration(

                      hintStyle: TextStyle(fontSize: 24,color: HexColor('#CCCCCC')),
                      border: InputBorder.none,
                      hintText: '6-16位密码'
                  ),
                ),
                Container(height: 1,color: HexColor('#EEEEEE'),margin: EdgeInsets.only(right: 15),),

                Container(
                  margin: EdgeInsets.only(top: 15),
                  child: TextField(
                    controller: tf2,
                    obscureText : true,
                    inputFormatters: [
                      WhitelistingTextInputFormatter(RegExp(
                          "[a-zA-Z]|[0-9]")),
                      LengthLimitingTextInputFormatter(16)
                    ],
                    style: TextStyle(fontSize: 24,color: HexColor('#242A36')),
                    decoration: InputDecoration(

                        hintStyle: TextStyle(fontSize: 24,color: HexColor('#CCCCCC')),
                        border: InputBorder.none,
                        hintText: '确认密码'
                    ),
                  ),
                ),
                Container(height: 1,color: HexColor('#EEEEEE'),margin: EdgeInsets.only(right: 15),),

                Container(
                  margin: EdgeInsets.only(top: 50,right: 25,left: 25),
                  width: Get.width,
                  height: 45,
                  child: RaisedButton(
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(22),
                    ),
                    color:Theme.of(context).primaryColor,
                    onPressed: ()async{
                      if(tf1.text.isEmpty){
                        // Toast.toast(Get.overlayContext,msg: '请输入密码');
                        BotToast.showText(text: '请输入密码');
                        return;
                      }
                      if(tf1.text!=tf2.text){
                        // Toast.toast(Get.overlayContext,msg: '请输入密码');
                        BotToast.showText(text: '两次密码不一样');
                        return;
                      }
                      if(tf1.text.length<6){
                        BotToast.showText(text: '请输入至少6-16位密码');
                        return;
                      }

                      requestDataWithSetPass();

                      // logic.requestDataSendCode(tf.text).then((value) {
                      //   if(value['errocode']==0){
                      //     Get.to(RegisterGetCodePage(tf.text));
                      //   }
                      // });
                    },
                    child: Text('确定',style: TextStyle(color: Colors.white),),
                  ),
                ),
              ],
            )
          )
        ],
      ),
    );
  }


  void requestDataWithSetPass()async{
    var params = {
        // 'project_name':tf3.text,
        // 'company_name':tf4.text,

      'name':widget.login,
      'mobile':widget.login,

      'password':tf1.text,
      'verifyCode':widget.verifyCode,
      'examapp':'是',

    };

    String  result = await DioUtils(url: await PersistentStorage().getStorage('urlStr')).post(Address.signup,data:params);
    var json = jsonDecode(result);
    if(json['errcode']==1010){
      print('注册失败');
    }else{
      Get.offAll(LoginModulePage());
    }

    BotToast.showText(text: json['message']);
  }

}
